/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.model;

import net.cellobject.javacodeformatter.log.FormatterLog;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;

public class FormatterJavaElement implements IFormatterItem {
	private final FormatterItemType type;

	private final IJavaElement element;

	private String name;

	public FormatterJavaElement(final FormatterItemType type, final IJavaElement element) {
		this.type = type;
		this.element = element;
	}

	public static FormatterJavaElement loadFavorite(final FormatterItemType type, final String info) {
		final IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(info));
		if (res == null) {
			return null;
		}
		final IJavaElement elem = JavaCore.create(res);
		if (elem == null) {
			return null;
		}
		return new FormatterJavaElement(type, elem);
	}

	public String getName() {
		if (name == null) {
			name = element.getElementName();
		}
		return name;
	}

	public void setName(final String newName) {
		name = newName;
	}

	public String getLocation() {
		try {
			final IResource res = element.getUnderlyingResource();
			if (res != null) {
				final IPath path = res.getLocation().removeLastSegments(1);
				if (path.segmentCount() == 0) {
					return "";
				}
				return path.toString();
			}
		} catch (final JavaModelException e) {
			FormatterLog.logError(e);
		}
		return "";
	}

	public boolean isFavoriteFor(final Object obj) {
		return element.equals(obj);
	}

	public FormatterItemType getType() {
		return type;
	}

	@Override
	public boolean equals(final Object obj) {
		return (this == obj) || ( (obj instanceof FormatterJavaElement) && element.equals( ((FormatterJavaElement)obj).element));
	}

	@Override
	public int hashCode() {
		return element.hashCode();
	}

	// For now, this is how we suppress a warning that we cannot fix
	// See Bugzilla #163093 and Bugzilla #149805 comment #14
	@SuppressWarnings("rawtypes")
	public Object getAdapter(final Class adapter) {
		return getAdapterDelegate(adapter);
	}

	private Object getAdapterDelegate(final Class<?> adapter) {
		if (adapter.isInstance(element)) {
			return element;
		}
		final IResource resource = element.getResource();
		if (adapter.isInstance(resource)) {
			return resource;
		}
		return Platform.getAdapterManager().getAdapter(this, adapter);
	}

	public String getInfo() {
		try {
			return element.getUnderlyingResource().getFullPath().toString();
		} catch (final JavaModelException e) {
			FormatterLog.logError(e);
			return null;
		}
	}
}
